<?php
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * @author for ForestDale: Joseph Chang, last modified: 4/9/14
 */

include_once('dbinfo.php');
include_once(dirname(__FILE__) . '/../domain/Day.php');
include_once(dirname(__FILE__) . '/../database/dbShifts.php');

/*function create_dbDay(){
    connect();
    mysql_query("DROP TABLE IF EXISTS dbDay");
    $result = mysql_query("CREATE dbDay (id TEXT NOT NULL, fname TEXT NOT NULL, lname TEXT NOT NULL, specialty TEXT NOT NULL, phoneNum VARCHAR(10), cellNum VARCHAR(10), SSN TEXT NOT NULL, DOB TEXT NOT NULL, age VARCHAR(3)");
    if (!result)
        echo mysql_error() . "Error creating dbDay table<br>";
} */

function add_day($day) {
    if (!$day instanceof Day)
        die("Error: add_client type mismatch");
    
    connect();
    $query = "SELECT * FROM dbClient WHERE id = '" . $day->get_id() . "'";
    $result = mysql_query($query);
    
    if (!$result)
    {
        error_log('ERROR on select in add_day '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    
    //if there's no entry for this id, add it
    if ($result == null || mysql_num_rows($result) == 0) {
         $insertQuery='INSERT INTO dbDay (id, month, day, dom, month_num, day_of_week, day_of_year, year, shifts)'
                . 'VALUES("' .
                $day->get_id() . '","' .
                $day->get_month() . '","' .
                $day->get_day() . '","' .
                $day->get_dom() . '","' .
                $day->get_month_num() . '","' .
                $day->get_day_of_week() . '","' .
                $day->get_day_of_year() . '","' .
                $day->get_year() . '","' .
                $day->get_shifts() . '","' .
                '");';
         error_log('insert query in add_client is '.$insertQuery);
         $insertRes = mysql_query($insertQuery);
        if (!$insertRes) {
            error_log('ERROR on insert in add_client ' . mysql_error());
            die('Invalid query: ' . mysql_error());
        }
        mysql_close();
        return true;
    }
    mysql_close();
    return false;
}

function remove_day($id) {
    connect();
    $query = 'SELECT * FROM dbDay WHERE id = "' . $id . '"';
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on select in remove_day '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if ($result == null || mysql_num_rows($result) == 0) {
        mysql_close();
        return false;
    }
    $query = 'DELETE FROM dbDay WHERE id = "' . $id . '"';
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on delete in add_day '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    mysql_close();
    return true;
}

// return a Day object with an array of Shift objects, or null if not found
function retrieve_day($id) {
    $theDay=null;
    connect();
    $query = "SELECT * FROM dbDay WHERE id = '" . $id . "'";
    error_log('in retrieve_day, query is '. $query);
    $result = mysql_query($query);
    if (!$result)
    {
        error_log('ERROR on select in retrieve_day '. mysql_error());
        die('Invalid query: ' . mysql_error());
    }
    if (mysql_num_rows($result) !== 1) {
        error_log('in retrieve_day, did not get exactly one record');
        mysql_close();
        return false;
    }
    $result_row = mysql_fetch_assoc($result);
    // var_dump($result_row);
      if ($result_row) {
            $shiftKeys = $result_row['shift'];
            $shiftKeys = explode("*", $shiftKeys);
            $shifts = array();
            foreach ($shiftKeys as $key) {
            	$temp = select_dbShifts($key);
                if ($temp instanceof Shift) {
                	$shifts[$temp->get_name()] = $temp;
             
                }
            }
    $theDay = make_a_day($result_row,$shifts);
  
  }
   return $theDay;
}

function make_a_day($result_row,$shifts) {
    $theDay = new Day(
                    $result_row['id'],
                    $result_row['month'],
                    $result_row['day'],
                    $result_row['dom'],
                    $result_row['month_num'],
                    $result_row['day_of_week'],
                    $result_row['day_of_year'],
                    $result_row['year'],
                    $shifts);
                 //   $result_row['shifts']);
   
    return $theDay;
}

?>

